var common = require('../../../utils/common.js');
var app = getApp();
var baseUrl = app.url;

Page({
    data: {
        files: [],
        datalist:{},
        settlementIndex:0,  
        settlement:['无','月结','现金',],
        repairIndex:0,  
        repair:['请选择','维修中','已完成','未完成'],
        descNum:0     
    },
    onLoad:function(options){
        var id = options.id;
        common.bestCurl('GET','/repair/detail',res=>{
            this.setData({datalist:res.data,repairIndex:res.data.repair_status})
        },{'id':id})
    },
    onShow:function () {
        myDate = new Date();
        Y = myDate.getFullYear();
        M = myDate.getMonth();
        m = ++ M;
        d = myDate.getDate();
        m=checkTime(m);
        function checkTime(i){
            if (i<10) {
                i="0" + i
            }
            return i
        }
        this.setData({shift_time:Y+'-'+m+'-'+d})
    },
    bindShiftChange: function (e) {
        this.setData({
            shift_time: e.detail.value
        })
    },
    bindSettleChange: function (e) {
        this.setData({
            settlementIndex: e.detail.value
        })
    },
    bindRepairChange: function (e) {
        this.setData({
            repairIndex: e.detail.value
        })
    },
    chooseImage: function (e) {
        var that = this;
        wx.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                that.setData({
                    files: that.data.files.concat(res.tempFilePaths)
                });
            }
        })
    },
    previewImage: function(e){
        wx.previewImage({
            current: e.currentTarget.id,
            urls: this.data.files
        })
    },
    clickWrongIcon:function(event){
        id = event.target.id;
        var obj = this.data.files;
        obj.splice(id,1);
        this.setData({files:obj})
    },
    descChange:function (event) {
        this.setData({descNum:event.detail.cursor});        
    }, 
    formSubmit:function (event) {
        id = event.currentTarget.dataset.id;
        var formData = event.detail.value;
        common.bestCurl('POST','/repair/revert',res=>{
            if(res.statusCode == 200){
                if(res.data.code == 1){
                    var urls = this.data.files;
                    var flag = 0;
                    if(urls.length>0){
                        wx.showToast({
                            title: '图片上传中',
                            icon: 'loading',
                            duration: 15000
                        });
                        let SESSID = wx.getStorageSync('PHPSESSID');
                        if(SESSID){
                            var PHPSESSID = '?PHPSESSID='+SESSID;
                        }else{
                            PHPSESSID = '?';
                        }
                        url=baseUrl+'/repair/add_file'+PHPSESSID;
                        for (var i = urls.length - 1; i >= 0; i--) {
                            wx.uploadFile({
                                url: url, 
                                filePath: urls[i],
                                name: 'uploadfile',
                                formData:{
                                    'id': id
                                },
                                success: function(res){
                                    flag++;
                                    if(urls.length == flag){
                                        wx.showToast({
                                            title: '车辆取回成功',
                                            icon: '', 
                                            duration: 5000
                                        });
                                        setTimeout(res=>{
                                            wx.redirectTo({
                                                url: '../list/list'
                                            })
                                        },2000)
                                    }      
                                }
                            })
                        }
                    }else{
                        wx.showToast({
                            title: '车辆取回成功',
                            icon: '',
                            duration: 5000
                        });
                        setTimeout(res=>{
                            wx.redirectTo({
                                url: '../list/list'
                            })

                        },2000)
                    }
                }else{
                    wx.showModal({
                        content: '车辆取回失败',
                        showCancel: false,
                    });
                }
            }
        },Object.assign({id:id},formData))   
    }    
});

